/**
 * Filename: BrowseActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/13 16:24
 */
package com.github.postsanf.yinian.activity;

import android.app.DownloadManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.DraweePagerAdapterhx;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.MultiTouchViewPager;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 浏览大图
 */
public class BrowseActivityHx extends BaseActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private MultiTouchViewPager vp_browse;                              // 整个切换视图
    private int position;                               // 位置
    private DraweePagerAdapterhx mAdapter;                               // 适配器
    private List<String> picUrlsList = new ArrayList<>();                            // 图片数据
    private SimpleDraweeView iv_vp_bg;
    private RelativeLayout rl_save_img;
    private boolean isMe = false;
    private String singleurl;
    private int totalsize = 0;
    private DownloadManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_browse);
        initData();
        initView();
        setData();
    }

    private void initData() {
        position = getIntent().getIntExtra(CommonConstants.YNPOSITION, 0);
        isMe = getIntent().getBooleanExtra(CommonConstants.YNISME, false);
        singleurl = getIntent().getStringExtra("singleurl");
        picUrlsList.add(singleurl);
    }

    private void initView() {
        //获取下载服务
        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        mToolbar = (Toolbar) findViewById(R.id.browse_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.btn_expend_white);
        vp_browse = (MultiTouchViewPager) findViewById(R.id.vp_browse);
        rl_save_img = (RelativeLayout) findViewById(R.id.rl_save_img);
        iv_vp_bg = (SimpleDraweeView) findViewById(R.id.iv_vp_bg);
        rl_save_img.setOnClickListener(this);
    }

    private void setData() {
        mAdapter = new DraweePagerAdapterhx(this, picUrlsList, false);
        vp_browse.setAdapter(mAdapter);
        totalsize = picUrlsList.size();
        if (totalsize > 1) {
            mToolbar.setTitle((position + 1) + "/" + totalsize);
        } else {
            mToolbar.setTitle(" ");
        }
        vp_browse.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                int index = arg0 % totalsize;
                mToolbar.setTitle((index + 1) + "/" + totalsize);
                doBlur(index);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        doBlur(position);
        vp_browse.setCurrentItem(position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_save_img:
                Long tsLong = System.currentTimeMillis() / 1000;
                String ts = "yn_" + tsLong.toString() + ".png";
                SnackBarUtils.makeShort(mToolbar, "正在下载...").show();
                downPic(mAdapter.getPic(vp_browse.getCurrentItem()), ts);
                break;
        }
    }

    private void downPic(String url, String name) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yinian/YiNian");
        if (!(file.mkdir() || file.isDirectory())) {
            file.mkdirs();
        }
        DownloadManager.Request down = new DownloadManager.Request(Uri.parse(url));
        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        down.setShowRunningNotification(false);
        down.setVisibleInDownloadsUi(false);
        down.setDestinationInExternalPublicDir("yinian/YiNian/", name);
        manager.enqueue(down);
    }

    private void doBlur(int pos) {
        String paths = YNUrlHelper.getBrowseBlur(this, mAdapter.getPic(pos));
        Uri uri = Uri.parse(paths);
        ImageOptHelper.showFrescoBlur(uri, iv_vp_bg, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_delete, menu);
        MenuItem action_share_single = menu.findItem(R.id.action_share_single);
        MenuItem action_delete_single = menu.findItem(R.id.action_delete_single);
        MenuItem action_delete_all = menu.findItem(R.id.action_delete_all);
        action_delete_single.setVisible(false);
        action_share_single.setVisible(false);
        action_delete_all.setVisible(false);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    // 友盟统计
    public void onResume() {
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        super.onResume();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
